function vmp = importvmpfromspms(maps, maptype, bbox, res, imeth)
% importvtcfromanalyze  - import several Analzye SPM map files
%
% FORMAT:       vmp = importvmpfromspms([maps [, type [, bbox, res [, imeth]]]])
%
% Input fields:
%
%       maps        char or cell array with SPM map filenames
%       type        type of map ('a' for auto, default)
%       bbox        optional 2x3 bounding box (default: small TAL box)
%       res         optional resolution (default: 3)
%       imeth       interpolation, {'linear'}, 'cubic', 'nearest'
%
% Output fields:
%
%       vmp         created VMP object

% Version:  v0.7f
% Build:    9030323
% Date:     Mar-03 2009, 11:02 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    isempty(maps) || ...
   (~iscell(maps) && ...
    ~ischar(maps))
    if mainver > 6
        msargs = {'MultiSelect', 'on'};
    else
        msargs = {};
    end
    [mapf, mapp] = uigetfile( ...
        {'*.hdr', 'SPM2 result map files (*.hdr)'; ...
         '*.nii', 'SPM5 result map files (*.nii)'}, ...
        'Please select the SPM map files to import...', '*.hdr', msargs{:});
    if isequal(mapf, 0) || ...
        isequal(mapp, 0)
        vmp = [];
        return;
    end
    if ~iscell(mapf)
        maps = strrep([strrep(mapp, '\', '/') '/' mapf], '//', '/');
    else
        maps = mapf;
        for mc = 1:numel(maps)
            maps{mc} = strrep([strrep(mapp, '\', '/') '/' maps{mc}], '//', '/');
        end
    end
end
if ~iscell(maps)
    if size(maps, 1) > 1
        maps = cellstr(maps);
    else
        maps = {maps};
    end
end
if nargin < 2 || ...
   ~ischar(maptype)
    maptype = 'a';
end
if nargin < 3 || ...
   ~isa(bbox, 'double') || ...
   ~isequal([2, 3], size(bbox))
    bbox = [44, 38, 44; 241, 193, 211];
end
if nargin < 4 || ...
   ~isa(res, 'double') || ...
    numel(res) ~= 1 || ...
   ~any((1:12) == res)
    res = 3;
end
if nargin < 5 || ...
   ~ischar(imeth) || ...
   ~any(strcmpi(imeth(:)', {'cubic', 'linear', 'nearest'}))
    imeth = 'linear';
else
    imeth = lower(imeth(:)');
end

% big try and catch
try
    vmps = cell(1, 1);
    vmp = newnatresvmp(bbox, res);
    vmps{1} = vmp;
    opts = struct;
    opts.interp = imeth;
    opts.maptype = maptype;
    vmp.ImportSPMMaps(maps, opts);
    vmp.Map = vmp.Map(2:end);
    vmp.NrOfMaps = numel(vmp.Map);
    bless(vmp, 1);
catch
    clearbvqxobjects(vmps);
    rethrow(lasterror);
end
